import config from '../conf/config.js'

let count = 0;
export default function request({
	url,
	method = "get",
	data,
	header
} = {}) {
	// 获取token
	let token = uni.getStorageSync("token");
	return new Promise((resolve, reject) => { //执行器函数
		uni.request({
			url: config.baseURL + url,
			data,
			method,
			header:{
				...header,
				token
			},
		}).then((response) => {
			let [error, res] = response;
			if (res.data.code == 200) {
				console.log(res.data);
				resolve(res.data)
			} else {
				if (res.data.code == "5000000") { //表示 未登录
					count++;
					uni.removeStorageSync('token');
					uni.removeStorageSync('userInfo');
					if(count == 1) {
						uni.showModal({
							title:'提示',
							content:'登录已过期，请重新登录',
							showCancel:false,
							confirmText:'去登录',//确定按钮的文字
							success(res) {
								// 用户点击了确定按钮
								if(res.confirm){
									count = 0;
									uni.navigateTo({
										url: '/subPages/login/login'
									})
								}
							}
						})
					}
				}
			}
		})
	})
}
